package com.pearl.jacksoncore.demo.databind.custom;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.type.LogicalType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**




 * @author TD
 * @version 1.0
 * @date 2024/4/11
 */
public class ListToStringJsonDeserializer extends StdDeserializer<List> {
    public static final ListToStringJsonDeserializer instance = new ListToStringJsonDeserializer();

    public ListToStringJsonDeserializer() {
        super(String.class);
    }

    /**
     * 反序列化
     *
     * @param jsonParser             解析器
     * @param deserializationContext 上下文
     * @return 反序列化后的值
     * @throws IOException
     */
    @Override
    public List<String> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {

        if (jsonParser.hasToken(JsonToken.VALUE_NULL)) { // null 返回空集合
            return new ArrayList<>();
        } else if (jsonParser.hasToken(JsonToken.VALUE_STRING)) { // 当前是String类型
            String textValue = jsonParser.getText();// 原值
            // 空值返回空集合
            if (textValue.isEmpty() || _isBlank(textValue)) {
                return new ArrayList<>();
            }
            // “,”分割，返回集合
            return StrUtil.split(textValue, StrUtil.COMMA, true, true);
        }
        throw JsonMappingException.from(jsonParser, "JSON有误");
    }

    public LogicalType logicalType() {
        return LogicalType.Textual;
    }

    public boolean isCachable() {
        return true;
    }

    public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
        return "";
    }

    public List<?> deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
        return this.deserialize(p, ctxt);
    }
}
